import { z } from "zod";
import { codeartsrepoRequest, buildUrl } from "../common/utils.js";
import { RepositorySchema, ProjectSchema, SearchResultSchema } from "../common/types.js";

// 获取用户的所有仓库
export const ListUserAllRepositoriesSchema = z.object({
  user_id: z.string().describe("用户ID"),
  page: z.number().optional().describe("页码，默认为1"),
  per_page: z.number().optional().describe("每页数量，默认为20"),
  search: z.string().optional().describe("搜索关键词"),
});

// 创建项目和仓库
export const CreateProjectAndRepositoriesSchema = z.object({
  project_name: z.string().describe("项目名称"),
  repo_name: z.string().describe("仓库名称"),
  template_id: z.string().optional().describe("模板ID"),
  import_members: z.boolean().optional().describe("是否导入项目成员"),
  visibility: z.enum(["private", "public"]).optional().describe("可见性"),
  description: z.string().optional().describe("描述"),
});

// 创建项目并fork仓库
export const CreateProjectAndForkRepositoriesSchema = z.object({
  project_name: z.string().describe("项目名称"),
  repo_name: z.string().describe("仓库名称"),
  source_project_id: z.string().describe("源项目ID"),
  source_repo_id: z.string().describe("源仓库ID"),
  import_members: z.boolean().optional().describe("是否导入项目成员"),
  visibility: z.enum(["private", "public"]).optional().describe("可见性"),
  description: z.string().optional().describe("描述"),
});

// 查询项目下的所有仓库
export const ShowAllRepositoryByTwoProjectIdSchema = z.object({
  project_id: z.string().describe("项目ID"),
  page: z.number().optional().describe("页码，默认为1"),
  per_page: z.number().optional().describe("每页数量，默认为20"),
  search: z.string().optional().describe("搜索关键词"),
});

// 分支关联工作项
export const AssociateIssuesSchema = z.object({
  project_id: z.string().describe("项目ID"),
  branch_id: z.string().describe("分支ID"),
  issues: z.array(z.string()).describe("工作项ID列表"),
});

// 功能实现
export async function listUserAllRepositories(
  userId: string,
  page: number = 1,
  perPage: number = 20,
  search?: string
) {
  const url = buildUrl("https://codeartsrepo.example.com/api/v2/users/repositories", {
    user_id: userId,
    page,
    per_page: perPage,
    search
  });

  const response = await codeartsrepoRequest(url);
  return z.array(RepositorySchema).parse(response);
}

export async function createProjectAndRepositories(options: z.infer<typeof CreateProjectAndRepositoriesSchema>) {
  const response = await codeartsrepoRequest("https://codeartsrepo.example.com/api/v2/projects", {
    method: "POST",
    body: {
      project_name: options.project_name,
      repo_name: options.repo_name,
      template_id: options.template_id,
      import_members: options.import_members,
      visibility: options.visibility || "private",
      description: options.description,
    },
  });
  
  return {
    project: ProjectSchema.parse(response.project),
    repository: RepositorySchema.parse(response.repository),
  };
}

export async function createProjectAndForkRepositories(options: z.infer<typeof CreateProjectAndForkRepositoriesSchema>) {
  const response = await codeartsrepoRequest("https://codeartsrepo.example.com/api/v2/projects/fork", {
    method: "POST",
    body: {
      project_name: options.project_name,
      repo_name: options.repo_name,
      source_project_id: options.source_project_id,
      source_repo_id: options.source_repo_id,
      import_members: options.import_members,
      visibility: options.visibility || "private",
      description: options.description,
    },
  });
  
  return {
    project: ProjectSchema.parse(response.project),
    repository: RepositorySchema.parse(response.repository),
  };
}

export async function showAllRepositoryByTwoProjectId(
  projectId: string,
  page: number = 1,
  perPage: number = 20,
  search?: string
) {
  const url = buildUrl(`https://codeartsrepo.example.com/api/v2/projects/${projectId}/repositories`, {
    page,
    per_page: perPage,
    search
  });

  const response = await codeartsrepoRequest(url);
  return SearchResultSchema.extend({
    items: z.array(RepositorySchema)
  }).parse(response);
}

export async function associateIssues(
  projectId: string,
  branchId: string,
  issues: string[]
) {
  const response = await codeartsrepoRequest(`https://codeartsrepo.example.com/api/v2/projects/${projectId}/branches/${branchId}/issues`, {
    method: "POST",
    body: {
      issues
    },
  });
  
  return response;
} 